<template>
  <div class="register-container">
    <div class="tile-decoration"></div>
    <div class="register-box">
      <div class="register-header">
        <img src="@/assets/bathroom-logo.svg" alt="Logo" class="logo" />
        <h2>卫生间管理系统</h2>
        <p class="register-subtitle">用户注册</p>
      </div>

      <el-form
        class="register-form"
        :model="registerForm"
        :rules="rules"
        ref="registerFormRef"
        label-position="top"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="registerForm.username"
            placeholder="请输入用户名"
            :prefix-icon="User"
            clearable
            class="bathroom-input"
          />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            v-model="registerForm.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            show-password
            class="bathroom-input"
          />
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
            placeholder="请再次输入密码"
            :prefix-icon="Lock"
            show-password
            class="bathroom-input"
          />
        </el-form-item>

        <el-form-item label="电子邮箱" prop="email">
          <el-input
            v-model="registerForm.email"
            placeholder="请输入电子邮箱"
            :prefix-icon="Message"
            clearable
            class="bathroom-input"
          />
        </el-form-item>

        <el-form-item label="手机号码" prop="phone">
          <el-input
            v-model="registerForm.phone"
            placeholder="请输入手机号码"
            :prefix-icon="Phone"
            clearable
            class="bathroom-input"
          />
        </el-form-item>

        <el-form-item label="用户角色" prop="role">
          <el-radio-group v-model="registerForm.role" class="role-group">
            <el-radio label="admin">管理员</el-radio>
            <el-radio label="guest">游客</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            class="register-button"
            :loading="loading"
            @click="handleRegister"
          >
            注册
          </el-button>
        </el-form-item>

        <div class="login-link">
          已有账号？<router-link to="/login">返回登录</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { User, Lock, Message, Phone } from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()
const registerFormRef = ref<FormInstance>()
const loading = ref(false)

// 表单数据
const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  email: '',
  phone: '',
  role: 'guest' // 默认为游客
})

// 验证密码是否一致
const validatePass = (rule: any, value: string, callback: any) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== registerForm.password) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

// 表单验证规则
const rules = reactive<FormRules>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于 6 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, validator: validatePass, trigger: 'blur' }
  ],
  email: [
    { required: false, type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  phone: [
    { required: false, pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ],
  role: [
    { required: true, message: '请选择用户角色', trigger: 'change' }
  ]
})

// 处理注册
const handleRegister = async () => {
  if (!registerFormRef.value) return
  
  await registerFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      
      // 模拟网络请求延迟
      setTimeout(() => {
        try {
          // 注册用户
          const success = userStore.registerUser({
            username: registerForm.username,
            password: registerForm.password,
            role: registerForm.role as 'admin' | 'guest',
            email: registerForm.email,
            phone: registerForm.phone
          })
          
          if (success) {
            ElMessage.success('注册成功！')
            router.push('/login')
          } else {
            ElMessage.error('用户名已存在，请更换用户名')
          }
        } catch (error) {
          console.error('注册失败:', error)
          ElMessage.error('注册失败，请稍后重试')
        } finally {
          loading.value = false
        }
      }, 800)
    } else {
      ElMessage.warning('请正确填写注册信息')
    }
  })
}
</script>

<style scoped>
.register-container {
  height: 100vh;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #e0f7fa;
  background-image: repeating-linear-gradient(45deg, rgba(25, 118, 210, 0.1) 0px, rgba(25, 118, 210, 0.1) 20px, transparent 20px, transparent 40px);
  position: relative;
  overflow: hidden;
}

/* 卫生间特色装饰元素 */
.register-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 10px;
  background: repeating-linear-gradient(
    90deg,
    #42a5f5,
    #42a5f5 20px,
    #1976d2 20px,
    #1976d2 40px
  );
}

/* 瓷砖装饰 */
.register-container .tile-decoration {
  position: absolute;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
}

.register-box {
  width: 450px;
  padding: 40px;
  background-color: rgba(255, 255, 255, 0.9);
  border: 2px solid #1976d2;
  border-top: 8px solid #1976d2;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  z-index: 1;
  position: relative;
}

.register-header {
  text-align: center;
  margin-bottom: 20px;
}

.logo {
  width: 80px;
  height: 80px;
  margin-bottom: 16px;
}

.register-header h2 {
  font-size: 24px;
  color: #1976d2;
  margin: 0;
  position: relative;
}

.register-subtitle {
  font-size: 16px;
  color: #64b5f6;
  margin-top: 5px;
}

.register-header h2::after {
  content: '💧';
  position: absolute;
  font-size: 14px;
  top: -5px;
  right: -15px;
  animation: drip 2s infinite;
}

@keyframes drip {
  0% { opacity: 0; transform: translateY(-5px); }
  50% { opacity: 1; }
  100% { opacity: 0; transform: translateY(10px); }
}

.register-form {
  margin-top: 20px;
}

.register-button {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
  background: linear-gradient(90deg, #1976d2, #42a5f5);
  border: none;
  position: relative;
  overflow: hidden;
}

.register-button::before {
  content: '';
  position: absolute;
  top: -10px;
  left: -10px;
  right: -10px;
  bottom: -10px;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transform: translateX(-100%);
  animation: water-flow 2s infinite;
}

@keyframes water-flow {
  0% { transform: translateX(-100%); }
  50%, 100% { transform: translateX(100%); }
}

.bathroom-input :deep(.el-input__wrapper) {
  background-color: rgba(224, 247, 250, 0.5);
  border: 1px solid #42a5f5;
  box-shadow: 0 0 0 1px rgba(66, 165, 245, 0.2);
}

.bathroom-input :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #1976d2;
  border-color: #1976d2;
}

.bathroom-input :deep(.el-input__inner)::placeholder {
  color: #64b5f6;
}

/* 添加瓷砖风格的输入框装饰 */
.el-form-item {
  position: relative;
}

.el-form-item::before {
  content: '';
  position: absolute;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: #42a5f5;
  left: -15px;
  top: 30px;
  transform: translateY(-50%);
}

.role-group {
  display: flex;
  gap: 20px;
}

.login-link {
  text-align: center;
  margin-top: 15px;
  font-size: 14px;
  color: #666;
}

.login-link a {
  color: #1976d2;
  text-decoration: none;
  font-weight: bold;
}

.login-link a:hover {
  text-decoration: underline;
}
</style>
